﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class AD_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AD_List));
        }

        //装载广告类别表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ShowNewsClassTree()
        {
            var jsonString = "";//返回的json字符串
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                DataTable dt = ADInfoBll.GetAdClass( CommonConst.DB_Name);
                jsonString = "<ul id=\"tt1\" class=\"easyui-tree tree\" animate=\"true\" dnd=\"false\">";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonString += "<li id=\"" + dt.Rows[i]["id"] + "\">";
                    jsonString += "<span>" + dt.Rows[i]["classname"] + "</span>";
                    jsonString += "</li>";
                }
                jsonString += "</ul>";
                jsonResult.Add("msg", jsonString);
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetAdClass()
        {
            StringBuilder builder=new StringBuilder();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                DataTable dt = ADInfoBll.GetAdClass( CommonConst.DB_Name);
                foreach (DataRow row in dt.Rows)
                {
                    builder.AppendFormat("<option value=\"{0}\" defSize=\"{2}\">{1}</option>", row["id"], row["classname"], row["defwidth"] + "_" + row["defheight"]);
                }
                jsonResult.Add("msg", builder.ToString());
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除广告分类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteClass(int adId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                ADInfoBll.DeleteAdClass(adId.ToString(), CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "删除广告分类");
                dicHandleLog.Add("obj_guid", "adid=" + adId.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("msg", "删除成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //修改/添加广告分类
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UpdateAdClass(int classId, string className,int width,int height)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                if (classId==0)
                {
                    ADInfoBll.InsertAdClass(className,width,height, CommonConst.DB_Name);
                    jsonResult.Add("msg", "添加成功！");
                    jsonResult.Add("isreload", true);
                }
                else
                {
                    ADInfoBll.UpdateAdClass(classId, className,width,height, CommonConst.DB_Name);
                    jsonResult.Add("msg", "修改成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount(string currentwhere)
        {
            return ADInfoBll.GetADInfoCount(currentwhere, CommonConst.DB_Name);
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData)
        {
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            DataTable dt = ADInfoBll.GetADInfoTable(int.Parse(DictList["currentpageindex"].ToString()) - 1, int.Parse(DictList["pagesize"].ToString()), "*", DictList["sorting"].ToString() + " " + DictList["sortdir"].ToString(), DictList["currentwhere"].ToString(), CommonConst.DB_Name);
            return JsonHelper.DataTableToJSON(dt);
        }

        //保存商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string OnSave(string jsonData)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            var adId = DictList["id"].ToString();
            DictList.Remove("id");
            try
            {
                string strName = "";
                if (adId=="")
                {
                    ADInfoBll.InsertADInfo(DictList, CommonConst.DB_Name);
                    strName = "添加广告";
                }
                else
                {
                    Dictionary<string, object> dicWhere = new Dictionary<string, object>();
                    dicWhere.Add("id",adId);
                    ADInfoBll.UpdateAdsInfo(DictList,dicWhere, CommonConst.DB_Name);
                    strName = "修改广告";
                }

                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", strName);
                dicHandleLog.Add("obj_guid", "adid=" + adId.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("msg", "保存成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteADInfo(int AD_Id)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = ADInfoBll.DeleteADInfo(AD_Id.ToString(), CommonConst.DB_Name);
                if (ii == 0)
                {
                    jsonResult.Add("msg", "删除不成功！");
                    jsonResult.Add("isreload", false);
                }
                else
                {
                    var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", "删除广告");
                    dicHandleLog.Add("obj_guid", "AD_Id=" + AD_Id.ToString());
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "删除成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

    }
}
